package com.tom.happy.mrs.chicken.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.tom.happy.mrs.chicken.R;

public class CountDownView extends View {

    private static final String TAG = CountDownView.class.getSimpleName();
    private static final int BACKGROUND_COLOR = Color.WHITE;
    private static final int OUTER_BORDER_COLOR = Color.RED;
    private static final float OUTER_BORDER_WIDTH = 20f;
    private static final int INNER_BORDER_COLOR = 0xff58c12a;

    private int mWidth = 200;
    private int mHeight = 200;
    private Paint backgroundPaint;
    private Paint outerBorderPaint;
    private Paint innerBorderPaint;
    private Bitmap mBitmap;

    private int startAngle = -90;
    private float progress = 0;

    // 一共120秒.
    private final int mTotalTimeMillis = 120*1000;

    private CountDownTimerListener listener;

    public CountDownView(Context context) {
        this(context, null);
    }

    public CountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        backgroundPaint = new Paint();
        backgroundPaint.setAntiAlias(true);
        backgroundPaint.setDither(true);
        backgroundPaint.setColor(BACKGROUND_COLOR);
        backgroundPaint.setStyle(Paint.Style.FILL);

        outerBorderPaint = new Paint();
        outerBorderPaint.setAntiAlias(true);
        outerBorderPaint.setDither(true);
        outerBorderPaint.setColor(OUTER_BORDER_COLOR);
        outerBorderPaint.setStrokeWidth(OUTER_BORDER_WIDTH);
        outerBorderPaint.setStyle(Paint.Style.STROKE);

        innerBorderPaint = new Paint();
        innerBorderPaint.setAntiAlias(true);
        innerBorderPaint.setDither(true);
        innerBorderPaint.setColor(INNER_BORDER_COLOR);
        innerBorderPaint.setStyle(Paint.Style.FILL);

        mBitmap = zoomImage(BitmapFactory.decodeResource(getResources(), R.drawable.second_hand), mWidth - OUTER_BORDER_WIDTH, mHeight - OUTER_BORDER_WIDTH);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 画背景
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, backgroundPaint);
        //画外圈边框
        RectF rectF = new RectF(OUTER_BORDER_WIDTH / 2, OUTER_BORDER_WIDTH / 2, mWidth - OUTER_BORDER_WIDTH / 2, mHeight - OUTER_BORDER_WIDTH / 2);
        canvas.drawArc(rectF, 0, 360, false, outerBorderPaint);
        //画内圈背景
        rectF.inset(OUTER_BORDER_WIDTH, OUTER_BORDER_WIDTH);
        canvas.drawArc(rectF, startAngle, progress, true, innerBorderPaint);
        canvas.save();
        canvas.translate(mWidth / 2, mHeight / 2);
        canvas.rotate(progress);
        canvas.translate(-mWidth / 2, -mHeight / 2);
        canvas.drawBitmap(mBitmap, OUTER_BORDER_WIDTH / 2, OUTER_BORDER_WIDTH / 2, null);
        canvas.restore();
    }

    public void start() {
        if (listener != null) {
            listener.onStartCount();
        }
        CountDownTimer countDownTimer = new CountDownTimer(mTotalTimeMillis, 200) {
            @Override
            public void onTick(long millisUntilFinished) {
                progress = ((mTotalTimeMillis - millisUntilFinished) / (mTotalTimeMillis * 1.0f)) * 360;
                Log.d(TAG, "progress:" + progress);
                invalidate();
            }

            @Override
            public void onFinish() {
                progress = 360;
                invalidate();
                if (listener != null) {
                    listener.onFinishCount();
                }
            }
        }.start();
    }

    public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
                                   double newHeight) {
        // 获取这个图片的宽和高
        float width = bgimage.getWidth();
        float height = bgimage.getHeight();
        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();
        // 计算宽高缩放率
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 缩放图片动作
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
                (int) height, matrix, true);
        return bitmap;
    }

    public void setCountDownTimerListener(CountDownTimerListener listener) {
        this.listener = listener;
    }

    public interface CountDownTimerListener {

        void onStartCount();

        void onFinishCount();
    }
}